package com.crm.service.crm.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.common.constant.NumberConstant;
import com.crm.model.common.dtos.PageResponseResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.common.enums.AppHttpCodeEnum;
import com.crm.model.crm.course.dtos.CourseDto;
import com.crm.model.crm.course.dtos.PageCourseDto;
import com.crm.model.crm.course.pojos.Course;
import com.crm.service.crm.course.mapper.CourseMapper;
import com.crm.service.crm.course.service.CourseService;
import com.crm.utils.NumbersUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper,Course> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 课程条件分页查询
     * @param dto
     * @return
     */
    public ResponseResult findCourseIdOrNameOrCrowdOrCreatedTimeOrSubject(PageCourseDto dto){
        //参数校验
        dto.checkParam();
        //1.构造查询条件
        LambdaQueryWrapper<Course> courseWrapper = new LambdaQueryWrapper<>();
        IPage<Course> page = new Page<>(dto.getPage(),dto.getSize());
        if (StringUtils.isNotBlank(dto.getName())) {
            courseWrapper.eq(Course::getName,dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getCourseId())) {
            courseWrapper.eq(Course::getCourseId,dto.getCourseId());
        }
        if (StringUtils.isNotBlank(dto.getSubject())) {
            courseWrapper.eq(Course::getSubject,dto.getSubject());
        }
        if (StringUtils.isNotBlank(dto.getAdaptCrowd())) {
            courseWrapper.eq(Course::getAdaptCrowd,dto.getAdaptCrowd());
        }
        if (dto.getCreatedTime() != null) {
            //获取时间参数
            LocalDate createdTime = dto.getCreatedTime();
            //获取当天最小时间
            LocalTime min = LocalTime.MIN;
            LocalDateTime begin = LocalDateTime.of(createdTime, min);
            //获取当天最大时间
            LocalTime max = LocalTime.MAX;
            LocalDateTime end = LocalDateTime.of(createdTime, max);
            courseWrapper.between(Course::getCreatedTime,begin,end);
        }
        //分页查询
        page = page(page,courseWrapper);
        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 创建课程
     * @param dto
     * @return
     */
    public ResponseResult addCourse(CourseDto dto){
        //调用参数校验方法
        ResponseResult result = checkParams(dto);
        if (result != null) {
            return result;
        }

        //添加课程
        Course course = new Course();
        BeanUtils.copyProperties(dto,course);
        //生成课程编号
        String courseId = getCourseId();
        course.setCourseId(courseId);
        course.setCreatedTime(LocalDateTime.now());
        course.setUpdatedTime(LocalDateTime.now());
        courseMapper.insert(course);
        return ResponseResult.okResult(null);
    }

    private String getCourseId() {
        //调用编号生成工具
        String courseId = NumbersUtils.GeneratingNumbers(NumberConstant.COURSE_HEAD,NumberConstant.COURSE_LENGTH);
        //判断课程编号是否重复
        LambdaQueryWrapper<Course> courseWrapper = new LambdaQueryWrapper<>();
        courseWrapper.eq(Course::getCourseId,courseId);
        Course course1 = courseMapper.selectOne(courseWrapper);
        if (course1 != null) {
            courseId =getCourseId();
        }
        return courseId;
    }

    /**
     * 添加课程参数校验
     * @param dto
     * @return
     */
    private ResponseResult checkParams(CourseDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (StringUtils.isBlank(dto.getAdaptCrowd())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (StringUtils.isBlank(dto.getSubject())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (StringUtils.isBlank(dto.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (StringUtils.isBlank(dto.getDescription())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getPrice() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        return null;
    }

    /**
     * 删除课程
      * @param courseId
     * @return
     */
    public ResponseResult deleteCourse(String courseId){
        //参数校验
        if (StringUtils.isBlank(courseId)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<Course> courseWrapper = new LambdaQueryWrapper<>();
        courseWrapper.eq(Course::getCourseId,courseId);
        Course course = courseMapper.selectOne(courseWrapper);
        if (course == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据课程编号删除课程
        courseMapper.delete(courseWrapper);
        return ResponseResult.okResult(null);
    }

    public ResponseResult updateCourse(CourseDto dto){
        //调用参数校验方法
        ResponseResult result = checkParams(dto);
        if (result != null) {
            return result;
        }
        if (StringUtils.isBlank(dto.getCourseId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<Course> courseWrapper = new LambdaQueryWrapper<>();
        courseWrapper.eq(Course::getCourseId,dto.getCourseId());
        Course courseOne = courseMapper.selectOne(courseWrapper);
        if (courseOne == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //根据课程编号修改课程
        Course course = new Course();
        BeanUtils.copyProperties(dto,course);
        course.setUpdatedTime(LocalDateTime.now());
        courseMapper.update(course,courseWrapper);
        return ResponseResult.okResult(null);
    }
}
